<template>
  <header class="app-header">
    <div class="container">
      <h1 class="logo"><RouterLink to="/">小兔鲜</RouterLink></h1>
      <AppHeaderNav></AppHeaderNav>
      <div class="search">
        <i class="iconfont icon-search"></i>
        <input type="text" placeholder="搜一搜" v-model="keyword" @keyup.enter="search" />
      </div>
      <!-- 头部购物车 -->
      <AppHeaderCart></AppHeaderCart>
    </div>
  </header>
</template>
<script>
import { ref } from 'vue'
import AppHeaderNav from './app-header-nav.vue'
import AppHeaderCart from './app-header-cart.vue'
import { useRouter } from 'vue-router'
export default {
  name: 'AppHeader',
  components: {
    AppHeaderNav,
    AppHeaderCart
  },
  setup() {
    // 搜索框信息
    const router = useRouter()
    const keyword = ref('')
    const search = e => {
      if (keyword.value) {
        router.push(`/search?keyword=${keyword.value}`)
        keyword.value = ''
        e.target.blur() // 让input框失去焦点
      }
    }
    return { keyword, search }
  }
}
</script>

<style scoped lang="less">
.app-header {
  background: #fff;
  .container {
    display: flex;
    align-items: center;
  }
  .logo {
    width: 200px;
    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url(../../assets/images/images/logo.png) no-repeat center 18px / contain;
    }
  }

  .search {
    width: 170px;
    height: 32px;
    position: relative;
    border-bottom: 1px solid #e7e7e7;
    line-height: 32px;
    .icon-search {
      font-size: 18px;
      margin-left: 5px;
    }
    input {
      width: 140px;
      padding-left: 5px;
      color: #666;
    }
  }
}
</style>
